Page({

  /**
   * 页面的初始数据
   */
  data: {
    showOpenBox: false,
    ci_rwdid: '',
    CatalogID: '',
    WorksID: '',
    CustomerID: '',
    table: [],
    baseimageurl: 'http://img0.wenes.cn:8099/Upload/Project/',
    imgurl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      ci_rwdid: options.ci_rwdid,
      CatalogID: options.CatalogID,
      WorksID: options.WorksID,
      CustomerID: options.CustomerID,
    })
    that.requestPic(that.data.ci_rwdid, that.data.CatalogID)
  },

  requestPic: function (ci_rwdid, CatalogID) {
    var that = this
    // 获取图片接口
    wx.request({
      url: 'https://api.wenes.cn/jd/GetImages?ci_rwdid=' + ci_rwdid + '&CatalogID=' + CatalogID,
      success: function (res) {
        console.log(res)
        var length = res.data.Table.length
        console.log(length)
        that.setData({
          table: res.data.Table
        })
      }
    })
  },

  // 点击上传图片
  add: function () {
    var that = this
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://api.wenes.cn/jd/UploadJDimages',
          filePath: tempFilePaths[0],
          name: 'Filedata',
          formData: {
            'qt': 'upload',
            'type': '1',
            'W': '500',
            'H': '300',
            'HW': 'HW',
            'WorksID': that.data.WorksID,
            'CatalogID': that.data.CatalogID,
            'UserId': that.data.CustomerID,
          },
          success: function (res) {
            console.log('上传结果：', res)
            if (res.statusCode == 200) {
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
              })
              // 实时刷新
              that.requestPic(that.data.ci_rwdid, that.data.CatalogID)
            } else {
              wx.showToast({
                title: '失败',
                icon: 'failure',
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  // 点击事件删除照片
  delete: function (e) {
    var that = this
    console.log(e.currentTarget.dataset)
    var detailid = e.currentTarget.dataset.detailid
    wx.request({
      url: 'https://api.wenes.cn/jd/DeleteJDimages?DetailID=' + detailid + '&filename=Filedata',
      success: function (res) {
        if (res.statusCode == 200) {
          console.log('删除成功：' + res.data)
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
          // 实时刷新
          that.requestPic(that.data.ci_rwdid, that.data.CatalogID)
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'failure',
            duration: 2000
          })
        }
      }
    })
  },

  showImage: function (e) {
    var that = this
    that.setData({
      showOpenBox: (!that.data.showOpenBox),
      imgurl: e.currentTarget.dataset.url
    })
    console.log(that.data.showOpenBox)
    console.log(e.currentTarget.dataset.url)
  },

  hideImage: function (e) {
    var that = this
    that.setData({
      showOpenBox: (!that.data.showOpenBox),
    })

  }



})